home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The World of Computer Software
/
The World of Computer Software.iso
/
snws190s.zip
/
UNBATCH.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-06-21
|
11KB
|
377 lines
/*
SNEWS 1.12
unbatch - quick and dirty news toss, no feeding of other sites
Copyright (C) 1991 John McCombs, Christchurch, NEW ZEALAND
john@ahuriri.gen.nz
PO Box 2708, Christchurch, NEW ZEALAND
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 1, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
See the file COPYING, which contains a copy of the GNU General
Public License.
USAGE: unbatch
-n means decompress the first batch, then stop before tossing
-v means verbose, tell what we are doing
*/
#include "defs.h"
#include "unbatch.h"
#define BUFFSIZE 16000
unsigned _stklen = 16384;
char *inbuf = NULL;
char *outbuf = NULL;
INFO my_stuff;
/*------------------------------- main --------------------------------*/
void main(int argc, char *argv[])
{
FILE *tmp_file;
static char name[256], in_name[256];
int done, no_toss;
int drive;
long required_disk, disk_free;
struct stat st;
struct dfree df;
struct ffblk ffblk;
fprintf(stderr, "UNBATCH: (%s)\n", VERSION);
/* if TRUE, then just uncompress, don't unbatch */
/*
inbuf = malloc(BUFFSIZE);
outbuf = malloc(BUFFSIZE);
if (!inbuf || !outbuf) {
fprintf(stderr, "not enough memory to set file buffers");
exit(-1);
} */
no_toss = (argc == 2) && (strncmp("-n", argv[1], 2) == 0);
if (!load_stuff()) {
fprintf(stderr, "Couldn't read rc info\n");
exit(1);
}
load_active_file();
free_ng();
sprintf(in_name, "%s*.*", my_stuff.incoming_dir);
done = findfirst(in_name, &ffblk, 0);
while (!done) {
sprintf(name, "%s%s", my_stuff.incoming_dir, ffblk.ff_name);
printf("unbatch: processing %s\n", name);
/*
* Check for enough room. We need:
*
* - more than twice the size of the batch for uncompressed
* batch, say 2.8
*
* - space for the articles.
*
* For example a 100k batch may require 280kb for the uncompressed
* batch, then another 280 kb for the articles.
*/
/*
stat(name, &st);
required_disk = (st.st_size*2.8) * 2;
drive = getdisk();
getdfree(drive+1, &df);
disk_free = (long)df.df_avail * (long)df.df_bsec * (long)df.df_sclus;
if (disk_free < required_disk){
fprintf(stderr, "unbatch: %ld bytes of disk req'd to unpack batch\n");
exit(1);
}
*/
/* uncompress the news batch and return a pointer to the temp file */
if ((tmp_file = decode_batches(name, no_toss)) != NULL) {
open_hist_file();
toss(tmp_file);
close_hist_file();
fclose(tmp_file);
unlink(name);
sprintf(name, "%s", my_stuff.temp_name);
unlink(name);
} else {
printf("could not unpack compressed news batch %s\n", name);
exit(1);
}
done = findnext(&ffblk);
}
close_active_file();
}
/*--------------------------- unpack the batch ------------------------*/
void toss(FILE *tmp_file)
{
/*
* Toss it into the appropriate files.
*
* TODO:
* - badly structured, sort it out
*/
FILE *out_file = NULL;
FILE *spool_file = NULL;
char buf[512], msg[256], subject[256], msg_id[256], *p;
char junk_group[] = "junk";
char newsgroups[256], nglist[256];
long where;
ACTIVE *gp;
time_t t;
int already_junked, in_header, posted;
strcpy(msg_id, "");
strcpy(subject, "-- no subject --\n");
strcpy(newsgroups, "");
rewind(tmp_file);
time(&t);
in_header = TRUE;
/* read the file */
while (fgets(buf, 511, tmp_file) != NULL) {
if (strstr(buf, "#! rnews") != NULL) {
if (strlen(newsgroups) != 0) {
strcpy(nglist, newsgroups+11);
p = strtok(newsgroups, " \r\n,:");
p = strtok(NULL, " \r\n,:");
/*
* For each newsgroup
* - open the text file
* - save the file pointer
* - append the message to it
* - close the file
* - open the index file
* - save the file pointer and the subject line
*/
posted = 0;
while (p != '\x00') {
out_file = open_out_file(p);
if (out_file) {
posted = 1;
where = ftell(out_file);
rewind(spool_file);
while (fgets(buf, 511, spool_file) != NULL)
fputs(buf, out_file);
fprintf(out_file, "\n@@@@END\n");
fclose(out_file);
out_file = open_index_file(p);
gp = find_news_group(p);
fprintf(out_file,"%08ld %08ld %09ld %s", where,
gp->hi_num, t, subject);
fclose(out_file);
}
p = strtok(NULL, " \r\n,:");
}
if (!posted) {
out_file = open_out_file(junk_group);
if (out_file) {
where = ftell(out_file);
rewind(spool_file);
while (fgets(buf, 511, spool_file) != NULL)
fputs(buf, out_file);
fprintf(out_file, "\n@@@@END\n");
fclose(out_file);
out_file = open_index_file(junk_group);
gp = find_news_group(junk_group);
fprintf(out_file,"%08ld %08ld %09ld %s", where,
gp->hi_num, t, subject);
fclose(out_file);
}
else {
fprintf(stderr,"\njunk group *must* exist!");
exit(-1);
}
}
add_hist_record(msg_id, nglist);
fclose(spool_file);
}
strcpy(subject, "-- no subject --\n");
in_header = TRUE;
if ((spool_file = fopen("$$item", "w+b")) == NULL) {
fprintf(stderr, "can't open temp article file %s\n", "$$item");
exit(1);
}
} else {
/* flag the end of the header */
if (strcmp(buf, "\n") == 0)
in_header = FALSE;
/* save the newsgroups line */
if (in_header) {
if (strnicmp(buf,"Message-ID:",11) == 0) {
strcpy(msg, buf);
p = strtok(msg, " \t\r\n"); p = strtok(NULL, " \t\r\n");
strcpy(msg_id, p);
}
if (strnicmp(buf,"Newsgroups:",11) == 0) strcpy(newsgroups,buf);
if (strnicmp(buf,"Subject:",8) == 0) {
strcpy(subject, buf+8);
}
}
/* add our system name to the path list */
if (strnicmp(buf, "Path:", 5) == 0) {
p = strtok(buf, " \t");
p = strtok(NULL, " \t");
fprintf(spool_file, "Path: %s!%s", my_stuff.my_site, p);
} else {
fprintf(spool_file, "%s", buf);
}
}
}
/* process the last one */
if (strlen(newsgroups) != 0) {
strcpy(nglist, newsgroups+11);
p = strtok(newsgroups, " \r\n,:");
p = strtok(NULL, " \r\n,:");
posted = 0;
while (p != '\x00') {
out_file = open_out_file(p);
if (out_file) {
posted = 1;
where = ftell(out_file);
rewind(spool_file);
while (fgets(buf, 511, spool_file) != NULL)
fputs(buf, out_file);
fprintf(out_file, "\n@@@@END\n");
fclose(out_file);
out_file = open_index_file(p);
gp = find_news_group(p);
fprintf(out_file,"%08ld %08ld %09ld %s", where, gp->hi_num, t, subject);
fclose(out_file);
}
p = strtok(NULL, " \r\n,:");
}
if (!posted) {
out_file = open_out_file(junk_group);
if (out_file) {
where = ftell(out_file);
rewind(spool_file);
while (fgets(buf, 511, spool_file) != NULL)
fputs(buf, out_file);
fprintf(out_file, "\n@@@@END\n");
fclose(out_file);
out_file = open_index_file(junk_group);
gp = find_news_group(junk_group);
fprintf(out_file,"%08ld %08ld %09ld %s", where, gp->hi_num, t, subject);
fclose(out_file);
}
}
add_hist_record(msg_id, nglist);
fclose(spool_file);
unlink("$$item");
}
}
/*--------------------------- unpack the batch ------------------------*/
FILE *decode_batches(char *fn, int no_toss)
{
/*
* take the batch, strip off the !cunbatch, and feed the file to
* uncompress, the opened uncompressed file is returned
*/
FILE *in_file, *out_file;
int res, buf_cnt;
char buf[1025];
char name[128];
buf_cnt = 0;
/* open the batch file */
strcpy(name, fn);
if ((in_file = fopen(name, "rb")) == NULL) {
printf("batchfile %s could not be opened\n", name);
return(NULL);
}
/* open the compressed */
sprintf(name, "%s.z", my_stuff.temp_name);
if ((out_file = fopen(name, "wb")) == NULL) {
printf("cannot open output file %s\n", name);
return(NULL);
}
while ((res = fread(buf, 1, 1024, in_file)) > 0) {
fwrite(buf_cnt == 0 ? buf+12 : buf, 1,
buf_cnt == 0 ? res-12 : res, out_file);
buf_cnt++;
}
fclose(in_file);
fclose(out_file);
unlink(my_stuff.temp_name);
sprintf(name, "compress -d %s.z", my_stuff.temp_name);
/* printf("unbatch: decompressing batch (%s)\n", name); */
system(name);
if (no_toss) return(NULL);
/* open the uncompressed */
sprintf(name, "%s", my_stuff.temp_name);
if ((out_file = fopen(name, "rb")) == NULL) {
printf("cannot open uncompressed file %s\n", name);
return(NULL);
}
return(out_file);
}